﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{
    public class TeamController : Controller
    {
        PlayerInterface playerInterface = new PlayerInterface();
        TeamInterface teamInterface = new TeamInterface();

        //
        // GET: /Team/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(teamInterface.GetTeamEditModel());
        }

        //
        // POST: /Team/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Index(TeamEditModel teamEditModel)
        {
            if (ModelState.IsValid)
            {
                teamInterface.Add(teamEditModel.NewTeam);
                teamInterface.Save();
                teamEditModel.PlayerA.TeamID = teamEditModel.NewTeam.TeamID;
                teamEditModel.PlayerB.TeamID = teamEditModel.NewTeam.TeamID;
                teamInterface.AddPlayerTeam(teamEditModel.PlayerA);
                teamInterface.AddPlayerTeam(teamEditModel.PlayerB);
                teamInterface.Save();

                if (String.IsNullOrEmpty(teamEditModel.NewTeam.TeamName))
                {
                    teamEditModel.NewTeam.TeamName = "Team " + teamEditModel.NewTeam.TeamID;
                    if (TryUpdateModel(teamEditModel.NewTeam)) teamInterface.Save();
                }
                return RedirectToAction("Index");
            }
            else
            {
                Team team = teamEditModel.NewTeam;
                teamEditModel = teamInterface.GetTeamEditModel();
                teamEditModel.NewTeam = team;
                return View(teamEditModel);
            }
        }

        //
        // GET: /Team/TeamEdit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult TeamEdit(int id)
        {
            return View("Index", teamInterface.GetTeamEditModel(id));
        }

        //
        // POST: /Team/TeamEdit/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult TeamEdit(TeamEditModel teamEditModel)
        {
            if (ModelState.IsValid)
            {
                var updateTeam = teamInterface.GetTeam(teamEditModel.NewTeam.TeamID);
                updateTeam.TeamName = teamEditModel.NewTeam.TeamName;

                if (TryUpdateModel(updateTeam))
                {
                    teamInterface.Save();

                    var updatePlayerA = teamInterface.GetPlayerTeam(teamEditModel.PlayerA.PlayerID, teamEditModel.NewTeam.TeamID);
                    var updatePlayerB = teamInterface.GetPlayerTeam(teamEditModel.PlayerB.PlayerID, teamEditModel.NewTeam.TeamID);
                    teamInterface.GetTeamPlayers(teamEditModel.NewTeam.TeamID).ToList().ForEach(delegate(Player player)
                    {
                        if (player.PlayerID != teamEditModel.PlayerA.PlayerID && player.PlayerID != teamEditModel.PlayerB.PlayerID)
                            teamInterface.DeletePlayerTeam(teamInterface.GetPlayerTeam(player.PlayerID, teamEditModel.NewTeam.TeamID));
                    });

                    if (updatePlayerA == null)
                        teamInterface.AddPlayerTeam(new PlayerTeam
                        {
                            PlayerID = teamEditModel.PlayerA.PlayerID,
                            TeamID = teamEditModel.NewTeam.TeamID
                        });

                    if (updatePlayerB == null)
                        teamInterface.AddPlayerTeam(new PlayerTeam
                        {
                            PlayerID = teamEditModel.PlayerB.PlayerID,
                            TeamID = teamEditModel.NewTeam.TeamID
                        });

                    return RedirectToAction("Index");
                }
            }
            Team team = teamEditModel.NewTeam;
            teamEditModel = teamInterface.GetTeamEditModel(team.TeamID);
            teamEditModel.NewTeam = team;
            return View("Index", teamEditModel);
        }

        //
        // GET: /Team/TeamActivate/5

        [Authorize(Roles = "Administrator")]
        public ActionResult TeamActivate(int id)
        {
            Team team = teamInterface.GetTeam(id);
            team.EndedOn = null;
            if (TryUpdateModel(team)) teamInterface.Save();

            return RedirectToAction("Index");
        }

        //
        // GET: /Team/TeamDeactivate/5

        [Authorize(Roles = "Administrator")]
        public ActionResult TeamDeactivate(int id)
        {
            Team team = teamInterface.GetTeam(id);
            team.EndedOn = DateTime.Now;
            if (TryUpdateModel(team)) teamInterface.Save();

            return RedirectToAction("Index");
        }

        //
        // POST: /Team/TeamDelete/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult TeamDelete(int id)
        {
            Team team = teamInterface.GetTeam(id);
            team.PlayerTeams.ToList().ForEach(delegate(PlayerTeam playerTeam) { teamInterface.DeletePlayerTeam(playerTeam); });
            teamInterface.Delete(team);
            teamInterface.Save();

            return RedirectToAction("Index");
        }

    }
}
